//
//  ThereViewController.m
//  5-31学习
//
//  Created by 钟纪旺 on 16/6/5.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import "ThereViewController.h"
#import "OneViewController.h"
@interface ThereViewController ()

@end

@implementation ThereViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    UILabel *l = [[UILabel alloc] init];
    l.text = @"UILabel测试";
    [l sizeToFit];
    l.center = CGPointMake(16 + l.frame.size.width/2, 240);
    l.textAlignment = NSTextAlignmentCenter;
    l.tag = 100;
    [self.view addSubview:l];
    UISegmentedControl * segmented = [[UISegmentedControl alloc] initWithItems:@[@"向左看", @"向前看", @"向右看"]];
    [segmented sizeToFit];
    segmented.center = CGPointMake(160, 190);
    [segmented addTarget:self action:@selector(onSeg:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmented];
//    // 添加图片
////    [segmented insertSegmentWithImage:[UIImage imageNamed:@"5"] atIndex:1 animated:NO];
////[segmented insertSegmentWithImage:[UIImage imageNamed:@"home"] atIndex:2 animated:NO];
////    [segmented insertSegmentWithImage:[UIImage imageNamed:@"my"] atIndex:3 animated:NO];
//    segmented.frame = CGRectMake(10.0, 10.0, 300.0  , 29.0);
//    segmented.selectedSegmentIndex = 0; //设置默认选中项
//    [segmented addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged]; //添加事件
//    [self.view addSubview:segmented];
}
- (void)onSeg:(UISegmentedControl *)seg
{
    UILabel *l = (UILabel *)[self.view viewWithTag:100];
   
    [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        if (seg.selectedSegmentIndex == 0) {
            l.center = CGPointMake(16 + l.frame.size.width/2, l.center.y);
        }else if(seg.selectedSegmentIndex == 1){
            l.center = CGPointMake(self.view.frame.size.width/2, l.center.y);
        }else{
            l.center = CGPointMake(self.view.frame.size.width - (16 + l.frame.size.width/2), l.center.y);
        }
    } completion:nil];
}

- (void) segmentedAction:(id)sender {
    if ([sender isKindOfClass:[UISegmentedControl class]]) {
        UISegmentedControl * segment = sender;
        if (segment.selectedSegmentIndex == 0) {
            self.view.backgroundColor = [UIColor redColor];
        }else if (segment.selectedSegmentIndex == 1) {
            self.view.backgroundColor = [UIColor brownColor];
        }else if (segment.selectedSegmentIndex == 2) {
            self.view.backgroundColor = [UIColor blueColor];
        }else if (segment.selectedSegmentIndex == 3){
            self.view.backgroundColor =[UIColor yellowColor];
        
        }else if (segment.selectedSegmentIndex ==4){
            OneViewController *abc =[OneViewController new];
            [self.navigationController pushViewController:abc animated:YES];
        
        }else {
            self.view.backgroundColor =[UIColor cyanColor];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
